package com.jinyuan.options;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.android.view.utils.ListViewHeightUtil;
import com.jinyuan.R;
import com.jinyuan.options.login.LoginActivity;
import com.jinyuan.options.login.LogoutActivity;
import com.jinyuan.view.NewFontsButton;
import com.jinyuan.view.slidingmenuview.adapter.SlidingMenuListViewAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * 左侧菜单控制类
 * 
 * @author azuryleaves
 * 
 */
public class LeftMenuActivity extends Activity {

	private static Callbacks defaultCallbacks = new Callbacks() {

		@Override
		public void onCommonMenuClick(AdapterView<?> adapterView, View view,
				int i, Object data) {
		}

		@Override
		public void onOperationMenuItemClick(AdapterView<?> adapterView,
				View view, int i, Object data) {
			
		}

	};
	
	public static Callbacks callbacks = defaultCallbacks;
	
	public static TextView leftmenu_username_TextView;
	private ListView leftmenu_commonmenu_ListView, leftmenu_operationmenu_ListView;
	public static NewFontsButton logout_Button;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.leftmenu);
		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		leftmenu_username_TextView = (TextView) findViewById(R.id.leftmenu_username_TextView);
		leftmenu_username_TextView.setLayoutParams(new LinearLayout.LayoutParams(windowManager.getDefaultDisplay().getWidth() / 4 * 3, ViewGroup.LayoutParams.WRAP_CONTENT));
		leftmenu_username_TextView.setOnClickListener(clickListener);
		leftmenu_commonmenu_ListView = (ListView) findViewById(R.id.leftmenu_commonmenu_ListView);
		leftmenu_operationmenu_ListView = (ListView) findViewById(R.id.leftmenu_operationmenu_ListView);
		logout_Button = (NewFontsButton) findViewById(R.id.leftmenu_logout_Button);
		logout_Button.setOnClickListener(clickListener);
		List<Map<String, Object>> commonData = new ArrayList<Map<String, Object>>();
		String[] commonTitles = getResources().getStringArray(R.array.leftmenu_commonlist);
		for (int i = 0; i < commonTitles.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("title", commonTitles[i]);
			commonData.add(map);
		}
		List<Map<String, Object>> operationData = new ArrayList<Map<String, Object>>();
		String[] operationTitles = getResources().getStringArray(R.array.leftmenu_operationlist);
		for (int i = 0; i < operationTitles.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("title", operationTitles[i]);
			operationData.add(map);
		}
		SlidingMenuListViewAdapter commonAdapter = new SlidingMenuListViewAdapter(
				this, commonData, R.layout.leftmenu_item,
				new String[] { "title" },
				new int[] { R.id.leftmenu_item_TextView });
		SlidingMenuListViewAdapter operationAdapter = new SlidingMenuListViewAdapter(
				this, operationData, R.layout.leftmenu_item,
				new String[] { "title" },
				new int[] { R.id.leftmenu_item_TextView });
		leftmenu_commonmenu_ListView.setAdapter(commonAdapter);
		leftmenu_operationmenu_ListView.setAdapter(operationAdapter);
		leftmenu_commonmenu_ListView
				.setOnItemClickListener(menuListViewListener);
		leftmenu_operationmenu_ListView
				.setOnItemClickListener(menuListViewListener);
		ListViewHeightUtil.setListViewHeight(leftmenu_commonmenu_ListView);
		ListViewHeightUtil.setListViewHeight(leftmenu_operationmenu_ListView);
	}
	
	private OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			SlidingMenuControlActivity.hideMenu();
			Intent intent = null;
			switch (view.getId()) {
			case R.id.leftmenu_username_TextView:
				if (LeftMenuActivity.logout_Button.getVisibility() == View.GONE) {
					intent = new Intent(LeftMenuActivity.this, LoginActivity.class);
					startActivity(intent);
				}
				break;
			case R.id.leftmenu_logout_Button:
				intent = new Intent(LeftMenuActivity.this, LogoutActivity.class);
				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};
	
	private OnItemClickListener menuListViewListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> adapterView, View view, int i,
				long l) {
			SlidingMenuControlActivity.hideMenu();
			switch (adapterView.getId()) {
			case R.id.leftmenu_commonmenu_ListView:
				callbacks.onCommonMenuClick(adapterView, view, i, adapterView	.getItemAtPosition(i));
				break;
			case R.id.leftmenu_operationmenu_ListView:
				callbacks.onOperationMenuItemClick(adapterView, view, i, adapterView.getItemAtPosition(i));
				break;
			default:
				break;
			}
		}

	};

	public interface Callbacks {

		/**
		 * OperationMenu列表项点击事件回掉接口
		 * 
		 * @param adapterView
		 * @param view
		 * @param i
		 */
		public void onOperationMenuItemClick(AdapterView<?> adapterView,
				View view, int i, Object data);

		/**
		 * CommonMenu列表项点击事件回掉接口
		 * 
		 * @param adapterView
		 * @param view
		 * @param i
		 */
		public void onCommonMenuClick(AdapterView<?> adapterView, View view,
				int i, Object data);

	}

}
